<?php
namespace app\api\model;
use think\Model;
use think\Db;
class Favorite extends Model{
    protected $updateTime = false;
    public function add($data){
        try {
            $id=$this->allowField(true)->save($data);
            if($id){
                return 1;
            }else{
                return 0;
            }
        }catch (\think\Exception $e){
            trace('insert failed'.$e->getMessage());
        }
    }
    public function deletes($id){
        try {
           $id=$this->destroy($id);
           if($id){
               return 1;
           }else{
               return 0;
           }
        }catch (\think\Exception $e){
            trace('delete exception'.$e->getMessage());
        }
    }
    
    public function deleteWhere($user_id,$place_id){
        try {
            $id=$this->where(array('user_id'=>$user_id,'place_id'=>$place_id))->delete();
            if(!$id){
                return 0;
            }else{
                return 1;
            }
        }catch (\think\Exception $e){
            trace('delete exception'.$e->getMessage());
        }
    }
    public function deleteAll($user_id){
        try {
            $id=$this->where(array('user_id'=>$user_id))->delete();
            if(!$id){
                return 0;
            }else{
                return 1;
            }
        }catch (\think\Exception $e){
            trace('delete exception'.$e->getMessage());
        }
    }
    /**
     * 查询我的收藏信息
     * 这里重新组合元素
     */
    public function selectLists($user_id){
       $res=Db::name('favorite')->where('user_id',$user_id)->select();
       $VenueModel=new Venue();
       if(!empty($res)){
           foreach ($res as $key=>$va){
               //dump($va['place_id']);
               $res[$key]['veninfo']=$VenueModel->VenueDetails_info($va['place_id']);
           }
         
           return $res;
       }else{
           return 0;
       }
    }
    /**
     * 用于通过点击登录状态 
     * 查询这个场地有无收藏
     */
    public function selectinfo($user_id,$place_id){
        try {
           $res=Db::name('favorite')->field('id')
            ->where(array('user_id'=>$user_id,'place_id'=>$place_id))
            ->find();
           if(!empty($res)){
               return $res['id'];
           }else{
               return 0;
           }
            
        }catch (\think\Exception $e){
            trace('select failed'.$e->getMessage());
        }
    }
}